<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class City extends \app\BaseModel
{
    protected $name = "massage_service_city_list";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function checkCity($input)
    {
        $dis[] = ["uniacid", "=", $input["uniacid"]];
        $dis[] = ["status", ">", -1];
        if(!empty($input["id"])) {
            $dis[] = ["id", "<>", $input["id"]];
        }
        $count = $this->where($dis)->where("city_type", "=", 1)->count();
        $dis[] = ["city_type", "=", $input["city_type"]];
        $dis[] = ["title", "=", $input["title"]];
        $data = $this->dataInfo($dis);
        if(!empty($data)) {
            return ["code" => 500, "msg" => "已有该城市"];
        }
        $num = getCityNumber($input["uniacid"]);
        if($num <= $count && $input["city_type"] == 1) {
            return ["code" => 500, "msg" => "超过授权城市数量，授权数量" . $num];
        }
        return true;
    }
    public function provinceInit($uniacid)
    {
        $dis = ["uniacid" => $uniacid, "city_type" => 1];
        $data = $this->where($dis)->where("status", ">", -1)->select()->toArray();
        $key = "province_data_int";
        incCache($key, 1, $uniacid, 99);
        $key_value = getCache($key, $uniacid);
        if($key_value == 1 && !empty($data)) {
            foreach ($data as $v) {
                $dis = ["uniacid" => $uniacid, "title" => $v["province"], "city_type" => 3, "city_code" => $v["province_code"], "status" => 1];
                $find = $this->dataInfo($dis);
                if(empty($find)) {
                    $insert = ["uniacid" => $uniacid, "city_type" => 3, "province" => $v["province"], "province_code" => $v["province_code"], "title" => $v["province"], "city_code" => $v["province_code"], "create_time" => time()];
                    $this->insert($insert);
                    $id = $this->getLastInsID();
                } else {
                    $id = $find["id"];
                }
                $this->dataUpdate(["id" => $v["id"]], ["pid" => $id]);
            }
        }
        decCache($key, 1, $uniacid);
        return true;
    }
    public function provinceDel()
    {
        $dis = ["status" => 1, "city_type" => 3];
        $data = $this->where($dis)->select()->toArray();
        if(!empty($data)) {
            foreach ($data as $v) {
                $find = $this->where(["pid" => $v["id"]])->where("status", ">", -1)->find();
                if(empty($find)) {
                    $this->dataUpdate(["id" => $v["id"]], ["status" => -1]);
                }
            }
        }
        return true;
    }
    public function cityData($admin)
    {
        $admin_model = new Admin();
        $admin["city_data"] = [];
        $city = $this->dataInfo(["id" => $admin["city_id"]]);
        if(!empty($city)) {
            if($admin["city_type"] == 1) {
                array_push($admin["city_data"], $city["pid"]);
                array_push($admin["city_data"], $admin["city_id"]);
                $admin["city"] = $city["title"];
                $admin["province"] = $this->where(["id" => $city["pid"]])->value("title");
                $admin["admin_ptitle"] = $admin_model->where(["id" => $admin["admin_pid"], "status" => 1, "city_type" => 3, "sub_agent_auth" => 1])->value("agent_name");
            } elseif($admin["city_type"] == 2) {
                $admin["area"] = $city["title"];
                $admin["city"] = $this->where(["id" => $city["pid"]])->value("title");
                $province_id = $this->where(["id" => $city["pid"]])->value("pid");
                $admin["province"] = $this->where(["id" => $province_id])->value("title");
                array_push($admin["city_data"], $province_id);
                array_push($admin["city_data"], $city["pid"]);
                array_push($admin["city_data"], $admin["city_id"]);
                $admin["admin_ptitle"] = $admin_model->where(["id" => $admin["admin_pid"], "sub_agent_auth" => 1, "status" => 1])->where("city_type", "in", [1, 3])->value("agent_name");
            } else {
                array_push($admin["city_data"], $admin["city_id"]);
                $admin["province"] = $city["title"];
            }
        }
        return $admin;
    }
}

?>